<?php
require_once 'pworks/mvc/result/PageConf.class.php';
require_once 'pworks/mvc/result/ModuleConf.class.php';

class PageXMLCfgLoader
{
	private $pageConf;

	public function load($filename)
	{
		if (file_exists($filename)) {
			$xml = simplexml_load_file($filename);
            $this->pageConf->BASE_DIR = (string) $xml['basedir']; 
            $this->pageConf->DEFAULT_SKIN = (string) $xml['default-skin'];
			foreach ($xml->children() as $page_node) {
                $pageConf = new pageConf;
                $pageConf->id = (string) $page_node['id'];
                $pageConf->title = (string) $page_node['title'];
                $pageConf->keyword = (string) $page_node['kw'];
                $pageConf->desc = (string) $page_node['desc'];
                $pageConf->skin = $page_node['skin'] ? (string) $page_node['skin'] : $this->pageConf->DEFAULT_SKIN; 
                $pageConf->template = (string) $page_node['template'];
                foreach ($page_node->children() as $node) {
                    $node_name = $node->getName();
                    switch ($node_name) {
                        case 'module':
                            $moduleConf = new ModuleConf;
                            $moduleConf->name = (string) $node['region'];
                            $moduleConf->value = (string) $node['value'];
                            foreach ($node->children() as $subnode) {
                                $param = (object) array('name' => '', 'value' => ''); 
                                $param->name = (string) $subnode['name'];
                                $param->value = (string) $subnode['value'];
                                $moduleConf->params[] = $param;
                            }
                            $pageConf->modules[] = $moduleConf;
                            break;
                    }
                }
				$this->pageConf->$page_node['id'] = $pageConf;
			}
		} else {
			$errorMessage = '['.__FILE__.', line:'.__LINE__.' ] Config file not found.';
			throw new SystemException(SystemException::FRAMEWORK_ERROR, $errorMessage);
		}
	}
	
	public function getConfig()
	{
		return $this->pageConf;
	}

    public function dump()
    {
        var_dump($this->pageConf);
    }
}

